#include "modeExit.hpp"

namespace qbe::state {

void ModeExitManager::add(AllModeType mode, const ExitCallback& callBack) {
  if (isModeType<DrawingModeType>(mode)) {
    drawing_mode_exitCall[getModeType<DrawingModeType>(mode)].push_back(callBack);
  }
  if (isModeType<EditingModeType>(mode)) {
    editing_mode_exitCall[getModeType<EditingModeType>(mode)].push_back(callBack);
  }
  if (isModeType<SelectModeType>(mode)) {
    select_mode_exitCall[getModeType<SelectModeType>(mode)].push_back(callBack);
  }
}

void ModeExitManager::exit(ecs::Query& query, AllModeType targetMode, AllModeType newMode) {
  if (isModeType<DrawingModeType>(targetMode)) {
    auto& quer_arr = drawing_mode_exitCall[getModeType<DrawingModeType>(targetMode)];
    for (auto& quer : quer_arr) {
      if (quer) {
        quer(query, newMode);
      }
    }
  }

  if (isModeType<EditingModeType>(targetMode)) {
    auto& quer_arr = editing_mode_exitCall[getModeType<EditingModeType>(targetMode)];
    for (auto& quer : quer_arr) {
      if (quer) {
        quer(query, newMode);
      }
    }
  }

  if (isModeType<SelectModeType>(targetMode)) {
    auto& quer_arr = select_mode_exitCall[getModeType<SelectModeType>(targetMode)];
    for (auto& quer : quer_arr) {
      if (quer) {
        quer(query, newMode);
      }
    }
  }
  cj_exitCall(query, newMode);
  return;
}

}  // namespace qbe::state